package com.bonc.apicreator.dao;


import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.bonc.apicreator.entity.RestApi;


/**
 * api的dao层接口
 *
 * @author gaopengyu
 * @version 2016年8月5日
 * @see ApiDao
 * @since
 */
@Transactional
public interface ApiDao extends JpaRepository<RestApi, Long> {
    /**
     * Description: 查询是否有该名字的api，用于重名校验
     * 
     * @param name
     * @return RestApi
     * @see
     */
    RestApi findByName(String name);

    /**
     * Description: 根据名字进行模糊查询，返回分页数据
     * 
     * @param proId
     * @param condition
     * @param pageable
     * @return Page<RestApi>
     * @see
     */
    Page<RestApi> findByProIdAndNameContaining(Long proId, String condition, Pageable pageable);

    /**
     * Description: 查询该工程下所有的api
     * 
     * @param proId
     * @param pageable
     * @return Page<RestApi>
     * @see
     */
    List<RestApi> findByProId(Long proId);

    /**
     * Description: 查询该工程下所有的api，返回分页数据
     * 
     * @param proId
     * @param pageable
     * @return Page<RestApi>
     * @see
     */
    @Query ("select api from RestApi api where proId=?1 order by api.moduleSort,api.moduleName,api.sort ")
    Page<RestApi> findByProId(Long proId, Pageable pageable);
    /**
     * Description: 查询重名url
     * 
     * @param name
     * @param proId
     * @return RestApi
     * @see
     */
    RestApi findByRequestUrlAndProId(String name, Long proId);
    

    /**
     * Description: 查询重名的API名
     * 
     * @param name
     * @param proId
     * @return RestApi
     * @see
     */
    RestApi findByNameAndProId(String name, Long proId);

    /**
     * Description: 按权重排序返回分页数据
     * 
     * @param proId
     * @param pageable
     * @return Page<RestApi>
     * @see
     */
    @Query ("select api from RestApi api where proId=?1 order by api.moduleSort,api.moduleName,api.sort ")
    Page<RestApi> findByProIdOrderBySort(Long proId, Pageable pageable);

    /**
     * Description: 按权重排序，按名称检索返回分页数据
     * 
     * @param proId
     * @param condition
     * @param pageable
     * @return Page<RestApi>
     * @see
     */
    Page<RestApi> findByProIdAndNameContainingOrderBySort(Long proId, String condition,
                                                          Pageable pageable);

    /**
     * Description: 按权重排序返回List
     * 
     * @param proId
     * @return List<RestApi>
     * @see
     */
    List<RestApi> findByProIdOrderBySort(Long proId);
    /**
     * 
     * Description:根据模块名字，查询API返回list并且根据权重排序
     * 
     *@param moduelName
     *@return List<RestApi>
     *
     * @see
     */
    List<RestApi> findByProIdAndModuleNameOrderBySort(Long proId,String moduelName);
}
